import { Book } from '@/types/book'
import { defineStore } from 'pinia'
import { ref } from 'vue'

const defaultImage = '/static/global/missing-face.png'

const initBook = {
  id: undefined,
  userId: undefined,
  bookAvatar: '',
  bookName: '',
  author: '',
  status: 0,
  description: '',
}

export const useBookStore = defineStore(
  'book',
  () => {
    const book = ref<Book>({ ...initBook })
    const bookList = ref<Book[]>([])

    const setBook = (val: Book) => {
      book.value = val
    }

    const clearBook = () => {
      book.value = { ...initBook }
    }

    const addBook = (book: Book) => {
      bookList.value.push(book)
    }

    const spliceBook = (index: number, book: Book) => {
      bookList.value.splice(index, 1, { ...book })
    }
    return {
      book,
      bookList,
      setBook,
      addBook,
      clearBook,
      defaultImage,
      spliceBook,
      initBook,
    }
  },
  {
    persist: true,
  },
)
